package com.berwin.bmfb.views;

import com.berwin.bmfb.entity.BMChar;
import com.berwin.bmfb.entity.BMFont;
import com.berwin.bmfb.utility.Utility;
import com.berwin.bmfb.views.components.Toast;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class PreviewView extends BasePanel {

    private BMFont bmFont = null;
    private JTextField tfPreviewTxt = null;

    private class Preview extends JPanel {

        private BMFont bmFont = null;
        private String text = null;
        private List<BMChar> bmChars = new ArrayList<>();

        public Preview(BMFont bmFont) {
            this.bmFont = bmFont;
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            int dx = 0, dy = 10; // 绘制的起点
            for (int i = 0; i < this.bmChars.size(); i++) {
                BMChar bmChar = this.bmChars.get(i);
                int x = bmChar.getX();
                int y = bmChar.getY();
                int w = bmChar.getWidth();
                int h = bmChar.getHeight();
                int ox = bmChar.getxOffset();
                int oy = bmChar.getyOffset();
                g.drawImage(bmChar.getImage(), dx + ox, dy + oy, dx + ox + w, dy + oy + h, 0, 0, w, h, null);
                dx += bmChar.getxAdvance();
            }
            // 绘制baseline
            g.drawLine(0, dy + this.bmFont.getBaseline(), this.getSize().width, dy + this.bmFont.getBaseline());
        }

        public void setText(String text) {
            this.text = text;
            this.bmChars.clear();
            for (int i = 0; i < text.length(); i++) {
                char c = text.charAt(i);
                BMChar bmChar = this._getBMCharByChar(String.valueOf(c));
                if (bmChar != null) {
                    this.bmChars.add(bmChar);
                } else {
                    PreviewView.this.mainView.getToast().popup("字符:" + c + " 未找到", Toast.error);
                }
            }
            this.repaint();
        }

        private BMChar _getBMCharByChar(String letter) {
            for (int i = 0; i < this.bmFont.getBmChars().size(); i++) {
                BMChar bmChar = this.bmFont.getBmChars().get(i);
                if (bmChar.getLetter().equals(letter)) {
                    return bmChar;
                }
            }
            return null;
        }

        public void setBmFont(BMFont bmFont) {
            this.bmFont = bmFont;
            this.repaint();
        }
    }

    private Preview preview = null;

    public PreviewView(MainView mainView, BMFont bmFont) {
        super(mainView);
        this.bmFont = bmFont;
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(this.getPreferredSize().width, 150));

        this.preview = new Preview(this.bmFont);
        // 中间
        JPanel center = Utility.makeBorderPanel("预览效果");
        center.setLayout(new BorderLayout());
        this.add(center, BorderLayout.CENTER);
        center.add(this.preview, BorderLayout.CENTER);

        // 下面
        JPanel south = Utility.makeBorderPanel("预览文字");
        south.setLayout(new BorderLayout());
        this.add(south, BorderLayout.SOUTH);

        tfPreviewTxt = new JTextField();
        south.add(tfPreviewTxt, BorderLayout.CENTER);
        tfPreviewTxt.getDocument().addDocumentListener(this);
//        tfPreviewTxt.setFocusable(true);
        tfPreviewTxt.setText(this.bmFont.getBMCharString());
    }


    @Override
    public void onUpdate() {
        this.preview.repaint();
    }

    @Override
    public void onSelectLetter(int idx) {
        this.isFromSelected = true;
        this.giveUpListenValueChange = true;
        this.isFromSelected = false;
        this.giveUpListenValueChange = false;
        this.onValueChanged();
    }

    @Override
    public void onValueChanged() {
        this.preview.setText(tfPreviewTxt.getText());
    }

    @Override
    public void onBMFontUpdated(BMFont bmFont) {
        this.bmFont = bmFont;
        this.giveUpListenValueChange = true;
        this.preview.setBmFont(bmFont);
        tfPreviewTxt.setText(this.bmFont.getBMCharString());
        this.preview.setText(this.bmFont.getBMCharString());
        this.giveUpListenValueChange = false;
    }
}
